package com.qf.xiaomi.web.servlet;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.xiaomi.pojo.TbGoodsType;
import com.qf.xiaomi.service.impl.TbGoodsTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Created by 马英超 on 2022/8/17 8:01
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class TypeServlet {
    protected String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final List <TbGoodsType> goodsTypeList = (List<TbGoodsType>) req.getServletContext().getAttribute("tbGoodsTypeList");

        //使用jkson转换为json数据
       /*    //创建ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();
          //反序列化到json--》对象转换为json字符串
        final String json = objectMapper.writeValueAsString(goodsTypeList);
        System.out.println(json);
        //反序列化到json对象--》json字符串转换成对象
       // objectMapper.readValue(json,List<TbGoodsType> goodsTypeList );
        */
        //使用FastJson实现json对象的转换
           //对象转换为json字符串
        final String json = JSON.toJSONString(goodsTypeList);
        System.out.println("json字符串：\t"+json);
        //使用fastjson将json字符串转换为对象
       /* final List<TbGoodsType> list = JSON.parseObject(json, List.class);
        System.out.println("json对象：\t"+list);
*/
        resp.setContentType("application/json;charset=UTF-8");//设置响应报文格式及字符编码
        //将转换后的json使用请求转发，到页面
        return json;
    }


}
